import 'package:flutter/material.dart';
import 'package:blurb/common/values/images.dart';
import 'package:blurb/common/routers/names.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import 'dart:math' as math;

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _leftLogoAnimation;
  late Animation<double> _rightLogoAnimation;
  late Animation<double> _opacityAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 2000),
      vsync: this,
    );

    _leftLogoAnimation = Tween<double>(begin: -1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.7, curve: Curves.easeOut),
      ),
    );

    _rightLogoAnimation = Tween<double>(begin: 1.0, end: 0.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.7, curve: Curves.easeOut),
      ),
    );

    _opacityAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.7, 1.0, curve: Curves.easeIn),
      ),
    );

    _controller.forward();

    // 动画完成后延迟跳转到主页
    Future.delayed(const Duration(milliseconds: 3000), () {
      context.go(RNS.main);
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {    
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage(MIP.background),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Logo动画部分
            SizedBox(
              height: 180.h,
              width: 250.w, // 增加宽度以容纳更大间距的logo
              child: Stack(
                alignment: Alignment.center,
                children: [
                  // 左半部分Logo
                  AnimatedBuilder(
                    animation: _controller,
                    builder: (context, child) {
                      return Transform.translate(
                        offset: Offset(_leftLogoAnimation.value * 100 - 27, 0),
                        child: Transform.rotate(
                          angle: _leftLogoAnimation.value * math.pi,
                          child: Opacity(
                            opacity: 1 - _leftLogoAnimation.value.abs(),
                            child: Image.asset(
                              MIP.logoLeft,
                              width: 90.w,
                              height: 90.h,
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                  // 右半部分Logo
                  AnimatedBuilder(
                    animation: _controller,
                    builder: (context, child) {
                      return Transform.translate(
                        offset: Offset(_rightLogoAnimation.value * 100 + 27, 0),
                        child: Transform.rotate(
                          angle: _rightLogoAnimation.value * math.pi,
                          child: Opacity(
                            opacity: 1 - _rightLogoAnimation.value.abs(),
                            child: Image.asset(
                              MIP.logoRight,
                              width: 90.w,
                              height: 90.h,
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
            SizedBox(height: 40.h),
            // Blurb标题
            AnimatedBuilder(
              animation: _opacityAnimation,
              builder: (context, child) {
                return Opacity(
                  opacity: _opacityAnimation.value,
                  child: Text(
                    'Blurb',
                    style: GoogleFonts.racingSansOne(
                      fontSize: 48.sp,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                );
              },
            ),
            SizedBox(height: 16.h),
            // 登录提示文字
            AnimatedBuilder(
              animation: _opacityAnimation,
              builder: (context, child) {
                return Opacity(
                  opacity: _opacityAnimation.value,
                  child: Text(
                    'Login to your account to continue',
                    style: GoogleFonts.racingSansOne(
                      fontSize: 18.sp,
                      color: Colors.white,
                    ),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}